package Survey;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class CModuleMultipleChoice extends CreatorModule{

	private static final long serialVersionUID = -7934237356008327369L;

	JTable options;

	public CModuleMultipleChoice(SurveyParameters data, Vector<Boolean> showing) {
		super(data, showing);

		if(data.dimensions == null){
			data.dimensions = new int[2];
			data.dimensions[SurveyParameters.HEIGHT] = 2;
			data.dimensions[SurveyParameters.WIDTH] = 4;
		}
		
		JPanel main2 = new JPanel();
		
		main2.setLayout(new BorderLayout());

		totalSize = new Dimension(SurveyCreatorGui.WIDTH - 20, 110 + 16 * data.dimensions[SurveyParameters.HEIGHT] );

		JPanel north = new JPanel();
		
		
		//types JCombobox
		String[] types= {"Radio Button", "Checkbox"};
		JComboBox  type = new JComboBox(types);
		if(data.type == SurveyParameters.MULTIPLE_CHOICE){
			((DefaultComboBoxModel)type.getModel()).setSelectedItem(type.getItemAt(1));
		}
		type.setMaximumSize(type.getPreferredSize());
		type.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				JComboBox temp = (JComboBox)e.getSource();
				((CModuleMultipleChoice)temp.getParent().getParent().getParent().getParent()).data.type = 
					((String)temp.getSelectedItem()).equals("Radio Button") ? SurveyParameters.SINGLE_CHOICE : SurveyParameters.MULTIPLE_CHOICE;
			}
		});
		JLabel dimensions = new JLabel("Dimensions: ");

		//height JTextfield
		JTextField height = new JTextField("" + data.dimensions[SurveyParameters.HEIGHT],2);
		height.setMaximumSize(height.getPreferredSize());
		height.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				try{
					int height = Integer.parseInt(((JTextField) e.getSource()).getText().trim());
					CModuleMultipleChoice module = ((CModuleMultipleChoice)((Component)e.getSource()).getParent().getParent().getParent().getParent());
					
					if(height <= 0){
						throw (new NumberFormatException());
					}
					
					if(height == module.options.getRowCount()){
						return;
					}
					
					module.data.dimensions[SurveyParameters.HEIGHT] = height;

					while(module.options.getRowCount() > height){
						((DefaultTableModel)module.options.getModel()).removeRow(((DefaultTableModel)module.options.getModel()).getRowCount()-1);
					}
					while(module.options.getRowCount() < height){
						((DefaultTableModel)module.options.getModel()).addRow((Vector<Object>)null);
					}
					
					
					//new String matrix
					String[][] working = new String[module.data.dimensions[SurveyParameters.HEIGHT]][module.data.dimensions[SurveyParameters.WIDTH]];
					for(int i = 0; i < working.length && i < module.data.options.length; i++){
						for(int j = 0; j < working[i].length && i < module.data.options[i].length; j++){
							working[i][j] = module.data.options[i][j];
						}
					}
					
					module.data.options = working;
					
					module.totalSize = new Dimension(SurveyCreatorGui.WIDTH - 20, 110 + 16*height);
					module.setPreferredSize(module.totalSize);
					module.setMinimumSize(module.getPreferredSize());
					((SurveyCreatorGui)module.getTopLevelAncestor()).updateSize();
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
						(((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.HEIGHT]) == 0 ?"":
						"" + ((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.HEIGHT]);
					}
			}
		});

		//identical to Actionlistener
		height.addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {return;}
			public void focusLost(FocusEvent e) {
				try{
					int height = Integer.parseInt(((JTextField) e.getSource()).getText().trim());
					CModuleMultipleChoice module = ((CModuleMultipleChoice)((Component)e.getSource()).getParent().getParent().getParent().getParent());
					
					if(height <= 0){
						throw (new NumberFormatException());
					}
					
					if(height == module.options.getRowCount()){
						return;
					}
					
					module.data.dimensions[SurveyParameters.HEIGHT] = height;

					while(module.options.getRowCount() > height){
						((DefaultTableModel)module.options.getModel()).removeRow(((DefaultTableModel)module.options.getModel()).getRowCount()-1);
					}
					while(module.options.getRowCount() < height){
						((DefaultTableModel)module.options.getModel()).addRow((Vector<Object>)null);
					}
					
					
					//new String matrix
					String[][] working = new String[module.data.dimensions[SurveyParameters.HEIGHT]][module.data.dimensions[SurveyParameters.WIDTH]];
					for(int i = 0; i < working.length && i < module.data.options.length; i++){
						for(int j = 0; j < working[i].length && i < module.data.options[i].length; j++){
							working[i][j] = module.data.options[i][j];
						}
					}
					
					module.data.options = working;
					
					module.totalSize = new Dimension(SurveyCreatorGui.WIDTH - 20, 68 + 16*height);
					module.setPreferredSize(module.totalSize);
					module.setMinimumSize(module.getPreferredSize());
					((SurveyCreatorGui)module.getTopLevelAncestor()).updateSize();
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
						(((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.HEIGHT]) == 0 ?"":
						"" + ((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.HEIGHT]);
					}
			}
		});

		JLabel by = new JLabel(" x ");

		//width
		JTextField width = new JTextField("" + data.dimensions[SurveyParameters.WIDTH], 2);
		width.setMaximumSize(width.getPreferredSize());
		width.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				try{
					int width = Integer.parseInt(((JTextField) e.getSource()).getText().trim());
					CModuleMultipleChoice module = ((CModuleMultipleChoice)((Component)e.getSource()).getParent().getParent().getParent().getParent());
					
					if(width <= 0 || width > 6){
						throw (new NumberFormatException());
					}
					
					if(width == module.options.getColumnCount()){
						return;
					}
					
					module.data.dimensions[SurveyParameters.WIDTH] = width;

					while(module.options.getColumnCount() > width){
						module.options.removeColumn(module.options.getColumnModel().getColumn(module.options.getColumnCount() - 1));
					}
					while(module.options.getColumnCount() < width){
						TableColumn  temp = new TableColumn();
						temp.setPreferredWidth(150);
						module.options.addColumn(temp);
					}
					
					
					//new String matrix
					String[][] working = new String[module.data.dimensions[SurveyParameters.HEIGHT]][module.data.dimensions[SurveyParameters.WIDTH]];
					for(int i = 0; i < working.length && i < module.data.options.length; i++){
						for(int j = 0; j < working[i].length && j < module.data.options[i].length; j++){
							working[i][j] = module.data.options[i][j];
						}
					}
					
					module.data.options = working;
					
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
							(((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.WIDTH]) == 0 ?"":
							"" + ((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.WIDTH]);	
				}
			}
		});

		//identical to actionlistener
		width.addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {return;}
			public void focusLost(FocusEvent e) {
				try{
					int width = Integer.parseInt(((JTextField) e.getSource()).getText().trim());
					CModuleMultipleChoice module = ((CModuleMultipleChoice)((Component)e.getSource()).getParent().getParent().getParent().getParent());
					
					if(width <= 0 || width > 6){
						throw (new NumberFormatException());
					}
					
					if(width == module.options.getColumnCount()){
						return;
					}
					
					module.data.dimensions[SurveyParameters.WIDTH] = width;

					while(module.options.getColumnCount() > width){
						module.options.removeColumn(module.options.getColumnModel().getColumn(module.options.getColumnCount() - 1));
					}
					while(module.options.getColumnCount() < width){
						TableColumn  temp = new TableColumn();
						temp.setPreferredWidth(150);
						module.options.addColumn(temp);
					}
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
						(((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.WIDTH]) == 0 ?"":
						"" + ((CModuleMultipleChoice)((Component) e.getSource()).getParent().getParent().getParent().getParent()).data.dimensions[SurveyParameters.WIDTH]);
				}
			}
		});

		JPanel center = new JPanel();
		if(data.options == null){
			data.options = new String[data.dimensions[SurveyParameters.HEIGHT]][data.dimensions[SurveyParameters.WIDTH]];
		}
		
		
		//the all-important grid
		options = new JTable(data.dimensions[SurveyParameters.HEIGHT], data.dimensions[SurveyParameters.WIDTH]){
			private static final long serialVersionUID = 7842463452327011404L;

			public TableCellRenderer getCellRenderer(int row, int column){	
				if(getValueAt(row, column) == null || ((String)getValueAt(row, column)).equals("")){
					return new MCTableCellRenderer(new Color(223,119,111),null);
				}
				if(((String)getValueAt(row, column)).startsWith("xx")){
					for(int i = 0; i < getRowCount(); i++){
						for(int j = 0; j < getColumnCount(); j++){
							if(!(i == row && j == column) && ((getValueAt(i,j) != null) &&((String)getValueAt(i, j)).startsWith("xx"))){
								return new MCTableCellRenderer(Color.red, null);
							}
						}
					}
				}
				
				return new MCTableCellRenderer(null,null);	
			

			}

		};
		for(int i = 0; i < data.dimensions[SurveyParameters.HEIGHT]; i++){
			for(int j = 0; j < data.dimensions[SurveyParameters.WIDTH]; j++){
				options.setValueAt(data.options[i][j], i, j);
			}
		}
		
		
		for(int i = 0; i< options.getColumnCount(); i++){
			options.getColumnModel().getColumn(i).setPreferredWidth(150);
		}
		
		options.setGridColor(Color.black);
		options.setBorder(BorderFactory.createMatteBorder(1, 1, 0, 0, Color.black));
		options.addFocusListener(new FocusListener(){

			public void focusGained(FocusEvent e) {return;}

			public void focusLost(FocusEvent e) {
				JTable table = (JTable)e.getSource();
				((CModuleMultipleChoice)table.getParent().getParent().getParent().getParent()).data.options = new String[table.getRowCount()][table.getColumnCount()];
				for(int i = 0; i < table.getRowCount(); i++){
					for(int j = 0; j < table.getColumnCount(); j++){
						((CModuleMultipleChoice)table.getParent().getParent().getParent().getParent()).data.options[i][j] = (String)table.getValueAt(i, j);
					}
				}

			}
		});

		north.setLayout(new BoxLayout(north, BoxLayout.X_AXIS));
		north.add(type);
		north.add(dimensions);
		north.add(height);
		north.add(by);
		north.add(width);
		north.setBackground(Color.white);
		north.setOpaque(true);

		center.add(options);
		center.setBackground(Color.white);
		center.setOpaque(true);

		main2.add(north, BorderLayout.NORTH);
		main2.add(center, BorderLayout.CENTER);
		
		main.add(main2);
		
		setPreferredSize(totalSize);
		setMaximumSize(getPreferredSize());
		
		if(!showing.get(data.index - 1).booleanValue()){
			main.setVisible(false);
			visible.setText("Show");
			setPreferredSize(SurveyCreatorGui.TOPSIZE);
			setMaximumSize(getPreferredSize());
		}
		
		setVisible(true);	
	}
	
	public void highlight(int i) {
		if(i%2 == 1){
			question.setBackground(Color.red);
		}else{
			question.setBackground(Color.white);
		}
		validate();
		repaint();
	}
}

class MCTableCellRenderer extends DefaultTableCellRenderer{
	private static final long serialVersionUID = 3138229310941869655L;

	private Color bground;
	private Color fground;

	public MCTableCellRenderer(Color background, Color text){
		if(background == null){
			bground = Color.WHITE;
		}else{
			bground = background;
		}

		if(text == null){
			fground = Color.BLACK;
		}else{
			fground = text;
		}
	}

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		cell.setBackground(bground);
		cell.setFont(cell.getFont().deriveFont(Font.BOLD));
		cell.setForeground(fground);

		return cell;
	}

	public Color getBackground(){
		return bground;
	}

	public Color getForeground(){
		return fground;
	}

}